﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using Excel;
namespace Exact_Extension
{
    public partial class ImportFinancialEntry : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        Common common;
        public ImportFinancialEntry(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            common = new Common(DS);
         

        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult resultDialog  = this.openFileDialog1.ShowDialog();
            if (resultDialog == DialogResult.OK)
            {
                this.txtFileImport.EditValue = this.openFileDialog1.FileName;
                FileInfo fileinfo = new FileInfo(this.txtFileImport.EditValue.ToString());
                FileStream stream = File.Open(this.txtFileImport.EditValue.ToString(), FileMode.Open, FileAccess.Read);
                IExcelDataReader excelReader = null;
                if (fileinfo.Extension == ".xls")
                {
                    //1. Reading from a binary Excel file ('97-2003 format; *.xls)
                    excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                }
                if (fileinfo.Extension == ".xlsx")
                {
                    //...
                    //2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
                    excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                }

                //4. DataSet - Create column names from first row
                excelReader.IsFirstRowAsColumnNames = true;
                DataSet result = excelReader.AsDataSet();

                //5. Data Reader methods
                List<FinEntry> ListFinEntry = new List<FinEntry>();
                int i = 0;
                while (excelReader.Read())
                {
                    if (i > 0)
                    {
                        FinEntry item = new FinEntry();
                        item.ReceiptDate = excelReader.GetDateTime(0);



                        item.StudentID = "";



                        item.DebtorCode = "";
                        item.InvoiceNo = "";
                        item.Description = excelReader.GetString(4);
                        item.GLAccount = excelReader.GetString(5);
                        
                         item.Amount = Convert.ToDouble(excelReader.GetString(6));

                        item.Currency = excelReader.GetString(7);
                        item.GLAccountOffset = excelReader.GetString(8);
                        item.JournalNumber = excelReader.GetString(1);

                        ListFinEntry.Add(item);
                    }
                    i++;
                }

                //6. Free resources (IExcelDataReader is IDisposable)
                excelReader.Close();
                this.finEntryBindingSource.DataSource = ListFinEntry;
            }
        }

        private void btnImport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                XMLExport xml = new XMLExport(DS);
                List<FinEntry> ListData = new List<FinEntry>();
                foreach (int i in this.gridView_Import.GetSelectedRows())
                {
                    ListData.Add((FinEntry)this.gridView_Import.GetRow(i));
                }
                string filename = xml.ExportFinancialEntryRevenue_OldSystem(ListData);
              //  xml.StartProcessImportFIN(filename);
               // MessageBox.Show("Import Financial Entries Bank/Cash sucessfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Import occured error , please contact administrator");
            }
        }
    }
}